﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using Home.Domain.Entities;
using Home.WebUI.Models;

namespace Home.WebUI.Controllers
{
    public class HomeController : Controller
    {
        private EFDbContext db = new EFDbContext();
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Products(int page = 1, int? ParentId = null, int? ChildId=null,int? BrandId=null)
        {
            #region 类别

            #region 类别列表
            var categorys = db.Categorys.Include(c => c.ChildEntitis).Include(c => c.Category_Brands.Select(ca => ca.Brand)).Where(c => c.ParentId == null);
            ViewBag.Categorys = categorys; 
            #endregion

            #region 导航
            //是否选择的子类
            if (ChildId != null)
            {
                var categoryname = db.Categorys.Include(c => c.ParentEntity).Where(c => c.Id == ChildId);
                ViewBag.Child = categoryname.First().Name;
                ViewBag.ChildId = categoryname.First().Id;
                ViewBag.Parent = categoryname.First().ParentEntity.Name;
                ViewBag.ParentId = categoryname.First().ParentEntity.Id;
            }
            else
                if(ParentId != null)
                {
                    ViewBag.Parent = db.Categorys.Where(ca => ca.Id == ParentId).Select(c => c.Name).First();
                    ViewBag.ParentId = ParentId;
                    if (BrandId!=null)
                    {
                        ViewBag.Brand = db.Brands.Where(b => b.Id == BrandId).Select(b => b.Name).First();
                        ViewBag.BrandId = BrandId;
                    }
                }
            #endregion

            #endregion

            #region 产品
            int pageSize = 15;
            var products = db.Products.OrderByDescending(c => c.CreateDate).Where(c => 1 == 1);
            //用于计算总数，分页用
            var count = db.Products.OrderByDescending(c => c.CreateDate).Where(c => 1 == 1);
            //大分类
            if (ParentId!=null)
            {                
                if (ChildId != null)
                {
                    count = count.Where(p => p.CategoryId == ChildId&&p.Category.ParentId==ParentId);
                    products = products.Where(p => p.CategoryId == ChildId && p.Category.ParentId == ParentId);
                }
                else
                {
                    //是否存在子分类
                    bool exitChild = Convert.ToBoolean(db.Categorys.Where(c => c.ParentId == ParentId).Count());
                    if (exitChild)
                    {
                        count = count.Where(p => p.Category.ParentId == ParentId);
                        products = products.Where(p => p.Category.ParentId == ParentId);
                    }
                    else
                    {
                        count = count.Where(p => p.Category.Id == ParentId);
                        products = products.Where(p => p.Category.Id == ParentId);
                    }                    
                }
            }
            if (BrandId!=null)
            {
                count = count.Where(p => p.BrandId == BrandId);
                products = products.Where(p => p.BrandId == BrandId);
            }
            
            products = products.Skip((page - 1) * pageSize).Take(pageSize);
            ViewBag.PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = count.Count() };
            
            ViewBag.Products = products;
             
            #endregion
            return View();
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
